import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:sprintf/sprintf.dart';
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;

class OneClassPage extends StatefulWidget{
  var BuildName;
  var ClassName;
  OneClassPage(String BuildName, String ClassName, {Key key}):super(key:key){
    this.BuildName = BuildName;
    this.ClassName = ClassName;
  }
  _OneClassPageState createState() => _OneClassPageState(this.BuildName, this.ClassName);
}

class _OneClassPageState extends State<OneClassPage>{
  var BuildName_CN;
  var BuildName_EN;
  var ClassName;
  var PicUrl_peopleChange;
  var PicUrl_personneldistribution;
  var LastUpdataTime;
  var LastestNum;
  EasyRefreshController _controller;

  _OneClassPageState(String BuildName, String ClassName){
    this.BuildName_CN = BuildName;
    switch(BuildName){
      case '知明':{
        BuildName = 'ZM';
      }break;
      case '笃行':{
        BuildName = 'XD';
      }break;
      case '立诚':{
        BuildName = 'LC';
      }break;
      case '致广':{
        BuildName = 'ZG';
      }break;
    }
    this.BuildName_EN = BuildName;
    this.ClassName = ClassName;
  }

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
    PicUrl_peopleChange = sprintf(
      "https://www.fjnu-study.cn/request/phpchart/examples/chart.php?type=OneClass&&build=%s&&class=%s",
        [this.BuildName_EN, this.ClassName]
    );
    PicUrl_personneldistribution = sprintf(
        "https://www.fjnu-study.cn/request/tempImg/pic_distribution.php?build=%s&&class=%s",
        [this.BuildName_EN, this.ClassName]
    );
    getdata();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        leading: new IconButton(
          icon: new Icon(Icons.arrow_back_ios),
          onPressed: () => Navigator.of(context).pop(),
        ),
        title: Text('${this.BuildName_CN} ${this.ClassName}'),
      ),
      body: EasyRefresh(
        controller: _controller,
        enableControlFinishLoad: true,
        enableControlFinishRefresh: true,
        child: Column(
          children: <Widget>[
            Text(
                '人数变化图',
                style: TextStyle(
                  fontSize: 20,
                  letterSpacing: 5,
                ),),
            Image.network(this.PicUrl_peopleChange),
            Text("数据最后一次更新于：${this.LastUpdataTime}"),
            Text("现在的人数:${this.LastestNum}"),
            Divider(),
            Text(
              '人员分布图',
              style: TextStyle(
                fontSize: 20,
                letterSpacing: 5,
              ),),
            Image.network(this.PicUrl_personneldistribution),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Image.asset("images/res/绿点.png"),
                    Text(":该同学保存了充分的社交距离👏")
                  ],
                ),
                Row(
                  children: <Widget>[
                    Image.asset("images/res/红点.png"),
                    Text(":该同学社交距离过近😕")
                  ],
                ),
              ],
            )
          ],
        ),
        onRefresh: () async{
          await Future.delayed(Duration(seconds: 1), () {
            setState(() {
              getdata();
            });
          });
          _controller.finishRefresh(success: true);
        },
      ),
    );
  }

  getdata() async{
    String url = "https://www.fjnu-study.cn/request/inf_request.php?type=OneClass&build=${this.BuildName_EN}&class=${this.ClassName}";
    var response = await http.get(url);
    if(response.statusCode == 200){
      var jsonRes = convert.jsonDecode(response.body);
      if(jsonRes['BuildName'] == this.BuildName_EN && jsonRes['Class'] == this.ClassName){
        List Data = jsonRes['data'];
        setState(() {
          this.LastestNum = Data[Data.length - 1]['num'];
          this.LastUpdataTime = Data[Data.length - 1]['time'];
        });
      }
    }
  }
}

